﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

namespace maptest.Common
{
    public class sql
    {
        public static string sqlConnect = ConfigurationManager.ConnectionStrings["MapConn"].ToString();
        SqlConnection con;
        SqlDataAdapter da;
        DataSet ds;
        SqlCommand cmd;
        public void connect()
        {
            if (con == null)
                con = new SqlConnection(sqlConnect);
            if (con.State == ConnectionState.Closed)
                con.Open();
        }
        public void disconnect()
        {
            if ((con != null) && (con.State == ConnectionState.Open))
                con.Close();
        }
        public DataSet get(string sql)
        {
            connect();
            da = new SqlDataAdapter(sql, con);
            ds = new DataSet();
            da.Fill(ds);
            disconnect();
            return ds;
        }
        public void ExeCuteNonQuery(string sql)
        {
            connect();
            cmd = new SqlCommand(sql, con);
            cmd.ExecuteNonQuery();
            disconnect();
        }
        public void UpdateStyleLayerMap(string tableLayer,string GeoSerializer)
        {
            connect();
            cmd = new SqlCommand();
            cmd.Parameters.Add("@style", SqlDbType.Text).Value = GeoSerializer;
            string sql = "UPDATE " + tableLayer + " SET style = @style";
            cmd.CommandText = sql;
            cmd.Connection = con;
            cmd.ExecuteNonQuery();
            disconnect();
        }
        public void CheckExistStyleColumn(string table)
        {
            connect();
            string query = "IF NOT EXISTS ( SELECT  * " +
                           "FROM    sys.columns " +
                           "WHERE   Name = N'style' " +
                           "AND Object_ID = OBJECT_ID(N'dbo."+table+"') ) " +
                           "BEGIN " +
                           "ALTER TABLE dbo."+table+" ADD style TEXT NULL " +
                           "END ";
            cmd=new SqlCommand(query,con);
            cmd.ExecuteNonQuery();
            disconnect();
        }
    }
}